<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <h1>开课吧异常监控</h1>
    <script src="https://cdn.bootcss.com/Base64/1.0.2/base64.js"></script>
    <script>
        // window.addEventListener('error', (...args) => {
        //     console.log('EventListener:', args, JSON.stringify(args[0].error))

        // })
        // window.onerror = (...args) => {
        //     console.log('window onerror', args)
        // }


        //         function uploadError({ lineno, colno, error: { stack }, timeStamp, message,filename }) {
        //     // 过滤
        //     const info = { lineno, colno, stack, timeStamp, message ,filename}

        //     console.log('error', info, JSON.stringify(info))
        //     const str = new Buffer(JSON.stringify(info)).toString("base64");

        //     console.log('str:', str)
        //     const host = 'http://localhost:7001/monitor/error'
        //     new Image().src = `${host}?info=${str}`

        // }

        // window.addEventListener('error', errorInfo => {
        //     console.log('EventListener:', errorInfo)

        //     uploadError(errorInfo)
        //     return true
        // }, true)



        //         aabc('123')

        function fun1() {
            console.log('1->begin')
            error
            console.log('1->end')
        }
        // window.onerror = (...args) => {
        //     console.log('onerror:', args)
        //     return true
        // }



        setTimeout(async () => {
            fun1()
            // new Promise((resolve, reject) => {
            //     abcxxx()
            // })
            // 增加异常捕获
            // .catch((err) => {
            //     console.log('promise catch:',err)
            // });
        })
        // PromiseError
        // setTimeout(async() => {
        //     const asyncFun = () => {
        //         error()
        //     }
        //     try {
        //         await asyncFun()
        //     } catch (e) {
        //         console.log('catch:',e)
        //     }
        // })
        setTimeout(() => {
            console.log('2->begin')
            console.log('2->end')
        })
        window.addEventListener("unhandledrejection", e => {
            throw e.reason
        });
        window.addEventListener('error', args => {
            console.log(
                'error event:', args
            );
            uploadError(args)
            return true;
        }, true);

        function uploadError({
            lineno,
            colno,
            error: {
                stack
            },
            timeStamp,
            message,
            filename
        }) {
            // 过滤
            const info = {
                lineno,
                colno,
                stack,
                timeStamp,
                message,
                filename
            }
            
            // const str = Base64.encode(JSON.stringify(info))
            const str = window.btoa(JSON.stringify(info))
            const host = 'http://localhost:7001/monitor/error'
            new Image().src = `${host}?info=${str}`
        }

        // new Image().src('./xxxx.png')
        // new Image().src = 'http://localhost:7001/monitor/error'+ '?info=xxxxxx'
    </script>
    <!-- <img src="./xxxxx.png"> -->
</body>

</html>